//
//  houxu.m
//  PASystem
//
//  Created by michael on 16/8/23.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "houxu.h"

@implementation houxu

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    UIColor *myColor = [UIColor colorWithWhite:0.8 alpha:0.3];
self.backgroundColor = myColor;


          }
    return self;
}
- (void)setId:(NSString *)id{


//NSLog(@"11%@",id);
_id = id;
 self.currentPage = 1;
 [self initview];

 
}

- (void)initview{
        self.ViewController.automaticallyAdjustsScrollViewInsets = YES;

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, KScreenWidth-20, (KScreenHeight-64-46-80)*2/3-20)];
view.backgroundColor= [UIColor whiteColor];

 UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0,0,KScreenWidth-20 , 20)];
    view1.backgroundColor = [UIColor whiteColor];
    UIView *viewcolor = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, view1.height)];
    viewcolor.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"图标3"]];
    [view1 addSubview:viewcolor];
     UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(viewcolor.right+20, viewcolor.top+5, 200, view1.height-5)];
          label.font = [UIFont systemFontOfSize:13];
label.textColor = [UIColor blueColor];
    label.text = @"企业回访及成果追踪 :";
    [view1 addSubview:label];
    
[view addSubview:view1];
_tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableview.frame = CGRectMake(0 , 20, KScreenWidth-20, (KScreenHeight-64-46-80)*2/3-20-20);

_tableview.backgroundColor = [UIColor whiteColor];

_tableview.dataSource = self;
_tableview.delegate = self;
//self.tableview.bounces = NO;
[view addSubview:_tableview];


[self addSubview:view];
   __weak typeof(self) weakSelf = self;


 [self.tableview addHeaderWithCallback:^{

        [weakSelf request]; //加载网络数据
    }];
     [self.tableview headerBeginRefreshing];
    
    [self.tableview addFooterWithCallback:^{
       
             [weakSelf loadMore];
             
    }];

 UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(10,view.bottom+10,KScreenWidth-20 , KScreenHeight-64-46-80-view.height-30)];
 view2.backgroundColor = [UIColor whiteColor];
  UIView *view11 = [[UIView alloc]initWithFrame:CGRectMake(0,0,KScreenWidth-20 , 20)];
    view11.backgroundColor = [UIColor whiteColor];
    UIView *viewcolor11 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, view11.height)];
    viewcolor11.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"图标3"]];
    [view11 addSubview:viewcolor11];
     UILabel *label11 = [[UILabel alloc]initWithFrame:CGRectMake(viewcolor11.right+20, viewcolor11.top+5, 200, view11.height-5)];
          label11.font = [UIFont systemFontOfSize:13];
label11.textColor = [UIColor blueColor];
    label11.text = @"座谈 :";
    [view11 addSubview:label11];
    [view2 addSubview:view11];
    
_view = [[houxuview alloc]initWithFrame:CGRectMake(0, 20, KScreenWidth-20, view2.height-20)];
_view.field = _id;
[view2 addSubview:_view];


    
    
 [self addSubview:view2];

  



}

- (void)request{
 self.currentPage = 1;

NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
 NSString *json =[NSString stringWithFormat: @"{'action':qyhfMch,'service':DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,_id,(long)self.currentPage];
     NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {

    NSArray *updic = JSON[@"reason"];

       _dataArray = [NSMutableArray array];
       
for (NSDictionary *dic in updic) {
    NSDictionary *dict = dic[@"attrs"];
    [_dataArray addObject:dict];
}
[_tableview reloadData];
 [self.tableview headerEndRefreshing];
  if (self.dataArray.count ==0) {
        [self makeToast:@"暂时还没有数据哦"];
        }
        self.currentPage++;

    } failure:^(NSError *error) {
         [self.tableview headerEndRefreshing];
    }];
}
#pragma mark 加载更多
-(void)loadMore{
   NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
 NSString *json =[NSString stringWithFormat: @"{'action':qyhfMch,'service':DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@,pageNum:%ld}'}",userId,pwdId,userHumber,_id,(long)self.currentPage];
      NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {

        self.currentPage++;
    
     NSArray *array = JSON[@"reason"];
        for (NSDictionary *dic in array) {
    NSDictionary *dict = dic[@"attrs"];
    [_dataArray addObject:dict];
    
}

        [self.tableview footerEndRefreshing];
                [self.tableview reloadData];

    } failure:^(NSError *error) {
         [self.tableview footerEndRefreshing];
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dic = _dataArray[indexPath.row];
 CGFloat titleSize = [OAToolBox sizebyText:[NSString stringWithFormat:@"%@",dic[@"CONTENT"]] font:12 minsWidth:(KScreenWidth-20)*.6];
  CGFloat titleSize2 = [OAToolBox sizebyText:[NSString stringWithFormat:@"%@",dic[@"FOLLOWSITUATION"]] font:12 minsWidth:(KScreenWidth-20)*.6];

if (titleSize<30) {
    titleSize = 30;
}
else{

 titleSize = titleSize;
}

if (titleSize2<30) {
    titleSize2 = 30;
}
else{

 titleSize2 = titleSize2;
}
float height = 180;



return height+titleSize+titleSize2;
}

#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 static NSString *CellIdentifier = @"Cell111111";  

    houxuTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell==nil) {  
        cell=[[houxuTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
   NSDictionary *dic = _dataArray [indexPath.row];
    cell.dic = dic;
    return cell;
}



@end
